<template>
  <div class="app-container">
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="primary" @click="handleAdd()">
          <i-ep-plus />
          参数对比
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        :data="tableData"
        highlight-current-row
        border
        height="calc(100vh - 120px - 100px)"
      >
        <el-table-column
          label="工段"
          prop="workshopSectionCode"
          min-width="70"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="工序"
          prop="processCode"
          min-width="65"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="生产线"
          prop="processEquipCode"
          min-width="70"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备"
          prop="equipName"
          min-width="100"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="零件名称"
          prop="partName"
          min-width="90"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="客户"
          prop="projectName"
          align="center"
          min-width="75"
          show-overflow-tooltip
        />
        <el-table-column
          label="型号描述"
          prop="productModelName"
          align="left"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品型号"
          prop="productModel"
          align="center"
          min-width="70"
          show-overflow-tooltip
        />
        <el-table-column
          label="程序名"
          prop="programName"
          min-width="70"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="参数名称"
          prop="parameterDesc"
          min-width="130"
          align="left"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="采集状态"
          prop="collectStatus"
          min-width="80"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            <span>{{
              useDictionary("CollectStatus", scope.row.collectStatus ?? "")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="最后采集时间"
          prop="lastCollectTime"
          width="165"
          align="center"
          show-overflow-tooltip
        /> -->
        <el-table-column
          label="参数类型"
          prop="processDataType"
          min-width="90"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag v-if="scope.row.processDataType == 2">加工参数</el-tag>
            <el-tag type="success" v-if="scope.row.processDataType == 3"
              >过程参数</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="监控数据类型"
          prop="monitorDataType"
          min-width="110"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag v-if="scope.row.monitorDataType == 1">动态量</el-tag>
            <el-tag type="success" v-if="scope.row.monitorDataType == 2"
            >常量</el-tag
            >
            <el-tag type="warning" v-if="scope.row.monitorDataType == 3"
            >补偿量</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="参考值"
          prop="parameterValue"
          min-width="70"
          align="right"
          show-overflow-tooltip
        />
        <el-table-column
          label="控制下限"
          prop="lowerLimitValue"
          min-width="74"
          align="right"
          show-overflow-tooltip
        />
        <el-table-column
          label="控制上限"
          prop="upperLimitValue"
          min-width="74"
          align="right"
          show-overflow-tooltip
        />
        <el-table-column
          label="采样频度"
          prop="samplingFrequency"
          min-width="100"
          align="center"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <div class="">
              <span>
                {{
                  useDictionary(
                    "ScaCollectFrequency",
                    row.samplingFrequency ?? ""
                  )
                }}
              </span>
              <span
                v-if="
                  row.collectionFrequencyTime && row.samplingFrequency == '2'
                "
                >&nbsp; {{ row.collectionFrequencyTime
                }}{{
                  row.collectionFrequencyUnit === "s"
                    ? "秒"
                    : row.collectionFrequencyUnit === "m"
                    ? "分钟"
                    : ""
                }}
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="生成工单"
          prop="isGenerateWorkOrder"
          min-width="74"
          align="center"
          show-overflow-tooltip
        >
          <template #default="{ $index, row }">
            <div class="flex justify-center">
              <el-switch
                v-model="row.isGenerateWorkOrder"
                disabled
                :active-value="1"
                :inactive-value="0"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="发送邮件"
          prop="isSendMail"
          min-width="74"
          align="center"
          show-overflow-tooltip
        >
          <template #default="{ $index, row }">
            <div class="flex justify-center">
              <el-switch
                v-model="row.isSendMail"
                disabled
                :active-value="1"
                :inactive-value="0"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="第一负责人"
          prop="responsiblePersonName"
          min-width="120"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="110" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
              >
                <i-ep-search />
              </el-button>
            </el-tooltip>

            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
              >
                <i-ep-edit />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="取消对比"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="deleteItem(scope.$index)"
              >
                <i-ep-circleClose />
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 特性项选择 -->
    <psmParamStatusContrast-select
      v-model:visible="selectDialog.visible"
      :data="breakList"
      @echo-info="changeVisible1"
    />

    <psmParamItemContrast-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => resetTableData(id)"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { mockList } from "./index";
import request from "@/utils/request";
import { useDictionary } from "@/utils/useTable";

defineOptions({
  name: "PSMPSMPARAMSTATUSCONTRAST",
  inheritAttrs: false,
});

const tableData = ref([]);
// const tableData = ref(mockList);

onMounted(() => {});

/** 添加行 */
const handleAdd = () => {
  selectDialog.value.visible = true;
  if (tableData.value.length > 0) {
    getBreakList();
  }
};

// 新增完返回的数据用于显示选择器
const breakList = ref(<any>[]);
const getBreakList = () => {
  breakList.value = tableData.value;
  //   console.log("回显的数据", breakList.value);
};

const selectDialog = ref({
  visible: false,
});
const changeVisible1 = (val: any) => {
  if (val && val.length) {
    tableData.value = val;
  } else {
    tableData.value = [];
  }
};

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
// 查询详情
async function getInfo(id: string) {
  request({
    url: `/dmpDosPcm/psmparammonitordetail/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开测量特性定义表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "编辑参数详情";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "参数详情-新增";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开测量特性定义表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "查看参数详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

// 搜索项折叠
const collapsed = ref(false);

async function deleteItem(index: any) {
  ElMessageBox.confirm("确认取消已选中的参数对比数据?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    ElMessage.success("取消成功！");
    tableData.value.splice(index, 1);
  });
}

onMounted(() => {});

function resetTableData(id) {
  request({
    url: `/dmpDosPcm/psmparammonitordetail/get/${id}`,
    method: "get",
  })
    .then((data) => {
      const currentIndex = tableData.value.findIndex((e) => e.id === id);
      tableData.value[currentIndex] = data.data;
    })
    .catch((res) => {
      //   console.log(res);
    });
}
</script>
<style lang="scss" scoped>
:deep(.table-container .el-table--default .cell) {
  padding: 0 5px;
}
</style>
